﻿package com.dnedelcu.robattle {
	
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import com.dnedelcu.robattle.Remoting;
	import com.dnedelcu.robattle.Player;
	import com.dnedelcu.robattle.Monster;
	import com.dnedelcu.robattle.GetStatsEvent;
	
	public class Robattle extends MovieClip {
		private var netConnection:NetConnection;
		private var rUser:Responder;
		private var rMonster:Responder;
		
		private var charID:int;
		private var monsterID:int;
		
		private var _remotingService:Remoting;
		
		private var _player:Player;
		private var _monster:Monster;
				
		private var userItemDrops:Array;
		
		private var itemsDropped:Boolean;
		
		public function Robattle() {
			_remotingService = new Remoting(this);
			charID = loaderInfo.parameters["charID"];
			monsterID = loaderInfo.parameters["monsterID"];
			_remotingService.createUserStats(charID);
			_remotingService.createMonsterStats(monsterID);
			userItemDrops = new Array();
			configureHandlers();
		}
		
		private function configureHandlers() {
			_remotingService.addEventListener(GetStatsEvent.USER_STATS_CREATED, onUserStatsCreated);
			_remotingService.addEventListener(GetStatsEvent.MONSTER_STATS_CREATED, onMonsterStatsCreated);
			_remotingService.addEventListener(GetStatsEvent.USER_STATS_UPDATED, onUserStatsUpdated);
			_remotingService.addEventListener(GetStatsEvent.ITEMS_STATS_RECEIVED, onItemStatsReceived);

			alertBox.visible = false;
			alertBoxBackground.visible = false;
			returnToExplore.visible = false;
			returnToExplore.label = "Explore again";
			returnToTownOnWin.visible = false;
			returnToTownOnWin.label = "Return to town";
			returnToTownOnLose.visible = false;
			returnToTownOnLose.label = "Return to town";
			character.visible = false;
			attack.label = "Attack";
		}
		
		private function onUserStatsCreated(event:GetStatsEvent):void {
			_player = new Player(event.userInfo);
			_player.addEventListener(GetStatsEvent.USER_ATTACKED, onUserAttacked);
			_player.addEventListener(GetStatsEvent.MONSTER_KILLED, onMonsterKilled);
			returnToTownOnWin.addEventListener(MouseEvent.CLICK, returnToTownEvent);
			returnToTownOnLose.addEventListener(MouseEvent.CLICK, returnToTownEvent);
			returnToExplore.addEventListener(MouseEvent.CLICK, returnToExploreEvent);

			playercurrenthp.width = Math.round(_player.percentHp * playercurrenthp.width);
			playercurrentmana.width = Math.round(_player.percentMana * playercurrentmana.width);
			characterLifeLabel.text = _player.currentLife + ' / ' + _player.maximumLife;
			characterManaLabel.text = _player.currentMana + ' / ' + _player.maximumMana;
			characterName.text = _player.characterName;
			attack.addEventListener(MouseEvent.CLICK, playerAttack);
		}

		function returnToTownEvent(event:MouseEvent):void {
			navigateToURL(new URLRequest("http://www.robattle.ro/character/town.php"), "_self");
		}
		
		function returnToExploreEvent(event:MouseEvent):void {
			navigateToURL(new URLRequest("http://www.robattle.ro/character/explore.php"), "_self");
		}
		
		private function playerAttack(event:MouseEvent):void {
			_player.Attack(_monster);
		}
		
		private function onMonsterStatsCreated(event:GetStatsEvent):void {
			_monster = new Monster(event.userInfo);
			_monster.addEventListener(GetStatsEvent.MONSTER_ATTACKED, onMonsterAttacked);
			_monster.addEventListener(GetStatsEvent.USER_KILLED, onUserKilled);
			monstercurrenthp.width = Math.round(_monster.percentHp * monstercurrenthp.width);
			monstercurrentmana.width = Math.round(_monster.percentMana * monstercurrentmana.width);
			monsterLifeLabel.text = _monster.currentLife + ' / ' + _monster.maximumLife;
			monsterManaLabel.text = _monster.currentMana + ' / ' + _monster.maximumMana;
			monsterName.text = _monster.characterName;
		}
		
		private function onUserStatsUpdated(event:GetStatsEvent):void {
			debug.htmlText += 'I made it<br />';
		}
		
		private function onUserAttacked(event:GetStatsEvent):void {
			monstercurrenthp.width = _monster.percentHp * monstercurrenthp.width;
			monstercurrentmana.width = _monster.percentMana * monstercurrentmana.width;
			monsterLifeLabel.text = _monster.currentLife + ' / ' + _monster.maximumLife;
			monsterManaLabel.text = _monster.currentMana + ' / ' + _monster.maximumMana;
			attack.visible = false;
			_monster.Attack(_player);
		}
		
		private function onMonsterAttacked(event:GetStatsEvent):void {
			playercurrenthp.width = _player.percentHp * playercurrenthp.width;
			playercurrentmana.width = _player.percentMana * playercurrentmana.width;
			characterLifeLabel.text = _player.currentLife + ' / ' + _player.maximumLife;
			characterManaLabel.text = _player.currentMana + ' / ' + _player.maximumMana;
			attack.visible = true;
		}
		
		private function onUserKilled(event:GetStatsEvent):void {
			playercurrenthp.width = _player.percentHp * playercurrenthp.width;
			playercurrentmana.width = _player.percentMana * playercurrentmana.width;
			characterLifeLabel.text = _player.currentLife + ' / ' + _player.maximumLife;
			characterManaLabel.text = _player.currentMana + ' / ' + _player.maximumMana;
			alertBox.htmlText += "You have been killed by " + _monster.characterName + ".<br />";
			alertBox.visible = true;
			alertBoxBackground.visible = true;
			returnToTownOnLose.visible = true;
			_remotingService.updateUserStats(_player.userInfo, charID);
		}
		
		private function onMonsterKilled(event:GetStatsEvent):void {
			monstercurrenthp.width = _monster.percentHp * monstercurrenthp.width;
			monstercurrentmana.width = _monster.percentMana * monstercurrentmana.width;
			monsterLifeLabel.text = _monster.currentLife + ' / ' + _monster.maximumLife;
			monsterManaLabel.text = _monster.currentMana + ' / ' + _monster.maximumMana;
			var monsterItemDrops = _monster.itemDrops;
			var randomNumber = getRandom(0, 100);
			var itemDrops:Array = new Array();
			itemsDropped = false;
			for (var i : * in monsterItemDrops) {
				if(monsterItemDrops[i][0] <= randomNumber) {
					itemsDropped = true;
					itemDrops.push(monsterItemDrops[i][1]);
				}
			}
			attack.visible = false;
			_remotingService.getItemsStats(itemDrops);
		}
		
		private function onItemStatsReceived(event:GetStatsEvent):void {
			var userInfo = event.userInfo;
			userItemDrops = userInfo;
			alertBox.htmlText += "You killed " + _monster.characterName + ".<br />";
			alertBox.htmlText += "<br />";
			alertBox.htmlText += "You found " + _monster.goldDrop + " gold on it." + "<br />";
			if(itemsDropped == true) {
				alertBox.htmlText += "You also found: ";
				alertBox.htmlText += "<ul>";
				for (var i:* in userItemDrops) {
					alertBox.htmlText += "<li><b>" + userItemDrops[i]['name'] + "</b></li>";
				}
				alertBox.htmlText += "</ul>";
			}
			alertBox.htmlText += "You aquired " + _monster.xp + " xp." + "<br />";
			alertBox.visible = true;
			alertBoxBackground.visible = true;
			returnToTownOnWin.visible = true;
			returnToExplore.visible = true;
			_player.currentXp = _monster.xp;
			if (_player.currentXp >= _player.maximumXp) {
				_player.currentXp = -_player.currentXp;
				_player.characterStatPoints = 10;
				_player.characterLevel = 1;
			}
			_player.gold = _monster.goldDrop;
			_player.userItemDrops = userItemDrops;
			_remotingService.updateUserStats(_player.userInfo, charID);
		}
		
		public function getRandom(lowNumber, highNumber):int {
			var thisNumber = highNumber - lowNumber;
			var randomUnrounded = Math.random() * thisNumber;
			var randomNumber = Math.round(randomUnrounded);
			randomNumber += lowNumber;
			return randomNumber;
		}
	}
}